﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestcraftLibrary.Common
{
    public class BinarySortedList<TValue> : IDictionary<double, TValue>
    {
        public List<KeyValuePair<double, TValue>> list;

        public BinarySortedList(int capacity)
        {
            this.list = new List<KeyValuePair<double, TValue>>(capacity);
        }

        private int IndexOfKey(double key)
        {
            int mid;
            int min = 0;
            int max = this.list.Count;

            mid = (min + max) / 2;

            while (min < max)
            {
                if (this.list[mid].Key < key)
                    min = mid + 1;
                else
                    max = mid;

                mid = (min + max) / 2;
            }

            return mid;
            //int min, max, mid;
            //mid = -1;
            //min = 0;
            //max = this.list.Count;

            //while (min < max)
            //{
            //    mid = (min + max) / 2;

            //    if (this.list[mid].Key < key)
            //        min = mid + 1;
            //    else
            //        max = mid;
            //}

            //return mid;
        }



        public void Add(double key, TValue value)
        {
            int index = this.IndexOfKey(key);
            this.list.Insert(index, new KeyValuePair<double, TValue>(key, value));
        }

        public bool ContainsKey(double key)
        {
            int index = this.IndexOfKey(key);
            return false;
        }

        public ICollection<double> Keys
        {
            get { throw new NotSupportedException(); }
        }

        public bool Remove(double key)
        {
            int index = this.IndexOfKey(key);
            this.list.RemoveAt(index);

            return true;
        }

        public bool TryGetValue(double key, out TValue value)
        {
            int index = this.IndexOfKey(key);
            value = this.list[index].Value;
            return true;
        }

        public ICollection<TValue> Values
        {
            get { throw new NotSupportedException(); }
        }

        public TValue this[double key]
        {
            get
            {
                int index = this.IndexOfKey(key);
                return this.list[index].Value;
            }
            set
            {
                int index = this.IndexOfKey(key);
                this.list[index] = new KeyValuePair<double, TValue>(key, value);
            }
        }

        public void Add(KeyValuePair<double, TValue> item)
        {
            
            int index = this.IndexOfKey(item.Key);

                this.list.Insert(index, item);
        }

        public void Clear()
        {
            this.list.Clear();
        }

        public bool Contains(KeyValuePair<double, TValue> item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(KeyValuePair<double, TValue>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { return this.list.Count; }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(KeyValuePair<double, TValue> item)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<KeyValuePair<double, TValue>> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}
